//
//  ResultResponseSerializer.m
//
//  Created by King-ChenXu on 14-10-13.
//  Copyright (c) 2014年 KK. All rights reserved.
//

#import "HFYJSONResponseSerializer.h"

static BOOL AFErrorOrUnderlyingErrorHasCodeInDomain(NSError *error, NSInteger code, NSString *domain) {
    if ([error.domain isEqualToString:domain] && error.code == code) {
        return YES;
    } else if (error.userInfo[NSUnderlyingErrorKey]) {
        return AFErrorOrUnderlyingErrorHasCodeInDomain(error.userInfo[NSUnderlyingErrorKey], code, domain);
    }
    
    return NO;
}

@implementation HFYJSONResponseSerializer

- (instancetype)init {
    self = [super init];
    if (!self) {
        return nil;
    }
    
    self.acceptableContentTypes = [NSSet setWithObjects:@"application/json", @"text/json", @"text/javascript",@"application/octet-stream",@"text/plain",@"text/html", nil];
    
    return self;
}


-(id)responseObjectForResponse:(NSURLResponse *)response data:(NSData *)data error:(NSError *__autoreleasing *)error
{
    if (![self validateResponse:(NSHTTPURLResponse *)response data:data error:error]) {
        if (!error || AFErrorOrUnderlyingErrorHasCodeInDomain(*error, NSURLErrorCannotDecodeContentData, AFURLResponseSerializationErrorDomain)) {
            return nil;
        }
    }
    
    NSError *jsonModelError ;
    NSMutableDictionary * result = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableLeaves error:&jsonModelError];
    NSString *string = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
    if (jsonModelError) {
        *error = [NSError errorWithDomain:TwokeNetworkingErrorDomain code:TwokeUnkownDataErrorCode userInfo:@{@"data":data}];
        return nil;
    }
    
//    
//    switch (result.code) {
//        case 1://表示成功
//            break;
//        case 2://请求错误
//            *error = [NSError errorWithDomain:TwokeNetworkingErrorDomain code:TwokeBadRequestErrorCode userInfo:@{@"msg":result.message}];
//            return nil;
//        case 3://用户身份验证信息错误
//            *error = [NSError errorWithDomain:TwokeNetworkingErrorDomain code:TwokeUserInvalidErrorCode userInfo:@{@"msg":result.message}];
//            return nil;
//        case 4://接口失效
//            *error = [NSError errorWithDomain:TwokeNetworkingErrorDomain code:TwokeRequestInvalidErrorCode userInfo:@{@"msg":result.message}];
//            return nil;
//        case 5://请求资源未找到
//            *error = [NSError errorWithDomain:TwokeNetworkingErrorDomain code:TwokeNotFoundErrorCode userInfo:@{@"msg":result.message}];
//            return nil;
//        case 6://您无权访问该资源
//            *error = [NSError errorWithDomain:TwokeNetworkingErrorDomain code:TwokePermissionDeniedErrorCode userInfo:@{@"msg":result.message}];
//            return nil;
//        case 7://服务器错误
//            *error = [NSError errorWithDomain:TwokeNetworkingErrorDomain code:TwokeBadServerErrorCode userInfo:@{@"msg":result.message}];
//            return nil;
//        default://未知状态码
//            *error = [NSError errorWithDomain:TwokeNetworkingErrorDomain code:TwokeBadServerErrorCode userInfo:@{@"msg":result.message}];
//            return nil;
//    }
    
    return result;
}



@end

